﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Shared
{
    public class SessionManager
    {
        #region Fields

        /// <summary>
        /// A list of session keys that's currently in use.
        /// </summary>
        private List<uint> KeyList = new List<uint>();

        #endregion

        #region Methods

        /// <summary>
        /// Generates a session key.
        /// </summary>
        /// <returns>Session Key</returns>
        public int Generate()
        {
            uint Key;

            while (true)
            {
                Key = (uint)new Random().Next(0, ushort.MaxValue * 5);

                if (!this.KeyList.Contains(Key))
                {
                    this.KeyList.Add(Key);
                    break;
                }
            }

            return (int)Key;
        }

        /// <summary>
        /// Destroys key from list.
        /// </summary>
        /// <param name="Key">Key to be destroyed.</param>
        public void Destroy(int Key)
        {
            this.KeyList.Remove((uint)Key);
        }

        #endregion
    }
}
